//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package upgrade

import (
	"context"

	"atomgit.com/openlinksaas/api-server/dao"
	"atomgit.com/openlinksaas/api-server/dao/code_dao"
)

type UpToVer39 struct{}

func (up *UpToVer39) Exec(ctx context.Context) error {
	threadItemList, err := code_dao.ThreadDao.ListAll(ctx)
	if err != nil {
		return err
	}
	for _, threadItem := range threadItemList {
		lastItem, err := code_dao.CommentDao.GetLastComment(ctx, threadItem.ProjectId, threadItem.ThreadId)
		if err != nil {
			continue
		}
		err = code_dao.ThreadDao.UpdateLastCommitId(ctx, threadItem.ProjectId, lastItem.ThreadId, lastItem.CommentId)
		if err != nil {
			return err
		}
	}
	return dao.VersionDao.Set(ctx, dao.VERSION_KEY_SCHEMA, 39)
}
